package com.hera.process.config.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hera.base.util.Constantes;
import com.hera.base.util.FiltroDTO;
import com.hera.process.config.dao.DepartmentDao;
import com.hera.process.config.dao.OrganizationDao;
import com.hera.process.config.domain.Department;
import com.hera.process.config.domain.Organization;
import com.hera.process.config.service.OrganizationService;

@Service("organizationservice")
public class OrganizationServiceImpl implements OrganizationService {

	static final Logger logger = Logger.getLogger(UserServiceImpl.class);
	
	@Autowired
	private OrganizationDao organizationdao;
	
	@Autowired
	private DepartmentDao departmentDao;
	
	@Override
	public Organization findById(Long id) {
		
		return organizationdao.findOne(id);
	}

	@Override
	public void update(Organization entity) {
		
		organizationdao.save(entity);
	}

	@Override
	public void save(Organization entity) {
		
		organizationdao.save(entity);
	}

	@Override
	public void delete(Organization entity) {
		//1.- Eliminamos los departamentos
		departmentDao.deleteByOrganization(entity.getIdOrganization());
		//2.- Eliminamos la organizacion
		organizationdao.delete(entity);
	}

	@Override
	public List<Organization> findAll() {
		
		return organizationdao.findAll();
	}

	
	public HashMap<Long,List<Department>> findDepartment(Organization organization) {
		FiltroDTO filtroOpcionesDTO=new FiltroDTO();
		filtroOpcionesDTO.addParametro("organization.idOrganization", organization.getIdOrganization());
		filtroOpcionesDTO.addOrdenamientoDescendente("department.idDepartment");
		List<Department> lstDepartment=departmentDao.obtenerEntidadesPorFiltros(filtroOpcionesDTO);
		
		HashMap<Long,List<Department>> opciones=new HashMap<Long, List<Department>>();
		List<Department> lstAuxiliar=null;
		for(Department department:lstDepartment){
			if(department.getDepartment()==null){//ES PADRE
				if(opciones.get(Constantes.MENU.ROOT)==null){//NO EXISTE EN LA RAIZ
					lstAuxiliar=new ArrayList<Department>();
					lstAuxiliar.add(department);
					opciones.put(Constantes.MENU.ROOT, lstAuxiliar);	
				}else{//existe
					lstAuxiliar=opciones.get(Constantes.MENU.ROOT);
					lstAuxiliar.add(department);
					opciones.put(Constantes.MENU.ROOT, lstAuxiliar);	
				}
				
			}else{// ES HIJO
				if(opciones.get(department.getDepartment().getIdDepartment())==null){
					lstAuxiliar=new ArrayList<Department>();
					lstAuxiliar.add(department);
					opciones.put(department.getDepartment().getIdDepartment(), lstAuxiliar);
				}else{
					lstAuxiliar=(List<Department>)opciones.get(department.getDepartment().getIdDepartment());
					lstAuxiliar.add(department);
					opciones.put(department.getDepartment().getIdDepartment(), lstAuxiliar);
				}
				
			}
		}
		return opciones;
	}
	
	@Override
	public List<Organization> findAll(FiltroDTO filtroDTO) {
		
		return organizationdao.obtenerEntidadesPorFiltros(filtroDTO);
	}

}
